package com.itema.controller;

import com.itema.anno.Log;
import com.itema.pojo.Dept;
import com.itema.pojo.Result;
import com.itema.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门信息
     */

    @GetMapping
    public Result list(){
        log.info("查询所有部门信息");
        List<Dept> deptList=deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 删除部门信息
     */
    @DeleteMapping
    @Log
    public Result delete(Integer id){
        log.info("删除部门信息:{}", id);//占位符替换
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加部门信息
     */
    @PostMapping
    @Log
    public Result add(@RequestBody Dept dept){
        log.info("添加部门信息:{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据Id查询
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("根据Id查询:{}", id);
        Dept dept=deptService.findById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门信息
     */
    @PutMapping
    @Log
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }
}
